package com.nbgroup.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.nbgroup.pojo.Clue;
import com.nbgroup.mapper.ClueMapper;
import com.nbgroup.pojo.Dict;
import com.nbgroup.pojo.common.PageBean;
import com.nbgroup.pojo.common.PageResult;
import com.nbgroup.pojo.dto.AssignmentOrGainClueDTO;
import com.nbgroup.pojo.dto.CluePageQueryDTO;
import com.nbgroup.pojo.dto.CluePoolPageQueryDTO;
import com.nbgroup.pojo.vo.ClueVO;
import com.nbgroup.service.IClueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-26
 */
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements IClueService {

    /**
     * 分页查询线索
     * @param dto
     * @return
     */
    @Override
    public PageBean pageQuery(CluePageQueryDTO dto) {
        // 构建分页条件
        Page<Clue> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Long id = dto.getId();
        String phone = dto.getPhone();
        String ownerName = dto.getOwnerName();
        Integer status = dto.getStatus();
        Integer channel = dto.getChannel();
        Map<String, Object> params = dto.getParams();
        if (params == null){
            params = Map.of();
        }
        // 构建查询条件
        Page<Clue> p = lambdaQuery()
                .eq(id != null,Clue::getId, id)
                .like(phone != null && !phone.equals(""),Clue::getPhone, phone)
                .like(ownerName != null&& !ownerName.equals(""),Clue::getOwnerName, ownerName)
                .eq(status != null,Clue::getStatus, status)
                .eq(channel != null,Clue::getChannel, channel)
                .ge(params.get("beginCreateTime") != "",Clue::getCreateTime,params.get("beginCreateTime"))
                .le(params.get("endCreateTime") != "",Clue::getCreateTime,params.get("endCreateTime"))
                .orderBy(true,false,Clue::getCreateTime)
                .page(page);
        // 封装VO结果
        List<ClueVO> clueVOS = BeanUtil.copyToList(p.getRecords(), ClueVO.class);
        // 返回结果
        PageBean pageBean = PageBean.builder().code(200).msg("查询成功").total(p.getTotal()).rows(clueVOS).build();
        return pageBean;
    }

    /**
     * 线索池分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult cluePoolPageQuery(CluePoolPageQueryDTO dto) {
        dto.check();
        Page<Clue> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<Clue> p = lambdaQuery().eq(dto.getId() != null,Clue::getId, dto.getId())
                .like(dto.getPhone() != null,Clue::getPhone, dto.getPhone())
                .eq(dto.getChannel() != null,Clue::getChannel, dto.getChannel())
                .ge(dto.getBeginCreateTime() != null,Clue::getCreateTime, dto.getBeginCreateTime())
                .le(dto.getEndCreateTime() != null,Clue::getCreateTime, dto.getEndCreateTime())
                .page(page);
        PageResult result = PageResult.builder().total(p.getTotal()).rows(p.getRecords()).build();
        return result;
    }

    /**
     * 线索捞取
     * @param dto
     * @return
     */
    //TODO:等待用户功能实现之后可以实现该接口
    @Override
    public Boolean gainClue(AssignmentOrGainClueDTO dto) {
        //判断捞取者已保有的线索数量
        // User user = Db.lambdaQuery(User.class).eq(User::getId,dto.getUserId()).one;
        // if (user.getClueCount >= 20){
        //     return false;
        // }
        lambdaUpdate().in(Clue::getClueId,dto.getIds()).set(Clue::getOwnerId,dto.getUserId()).update();
        return true;
    }
}
